/**
 * 作者: hxl
 * 日期: 2025-01-20 17:01:47
 * 备注: 用户在线事件表
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserOnlineEventInfo;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;

public class UserOnlineEventInfoModel extends BaseModel implements Model {
    public static final String LISTKEY = "";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = true;// 是否需要额外的有序列表

    protected long id; // 玩家id
    protected long lastEventTime; // 上次生成事件时间
    protected int lastGetStaff; // 最后获得的干员或秘书ID

    public void reset() {
    }

    protected String getKey() {
        return "UserOnlineEventInfo." + id;
    }

    protected String getListKey() {
        return null;
    }

    protected String getListItemKey() {
        return null;
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_online_event_info"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, id, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, id, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // 玩家id
        obj.put("lastEventTime", lastEventTime); // 上次生成事件时间
        obj.put("lastGetStaff", lastGetStaff); // 最后获得的干员或秘书ID
        return obj;
    }

    public PBUserOnlineEventInfo.Builder takeInitBuilder() {
        PBUserOnlineEventInfo.Builder sb = PBUserOnlineEventInfo.newBuilder();
        if (lastEventTime != 0) {
            sb.setLastEventTime(lastEventTime);
        }
        return sb;
    }

    public PBUserOnlineEventInfo.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserOnlineEventInfo.Builder sb = PBUserOnlineEventInfo.newBuilder();
        if (isFieldUpdate("lastEventTime")) {
            sb.setLastEventTime(lastEventTime);
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public long getLastEventTime() {
        return lastEventTime;
    }

    public void putLastEventTime(long lastEventTime) {
        if (this.lastEventTime != lastEventTime) {
            this.lastEventTime = lastEventTime;
            updateRedisHashField(getKey(), "lastEventTime", this.lastEventTime);
        }
    }

    public void addLastEventTime(long value) {
        this.lastEventTime += value;
        updateRedisHashField(getKey(), "lastEventTime", this.lastEventTime);
    }

    public int getLastGetStaff() {
        return lastGetStaff;
    }

    public void putLastGetStaff(int lastGetStaff) {
        if (this.lastGetStaff != lastGetStaff) {
            this.lastGetStaff = lastGetStaff;
            updateRedisHashField(getKey(), "lastGetStaff", this.lastGetStaff);
        }
    }

    public void addLastGetStaff(int value) {
        this.lastGetStaff += value;
        updateRedisHashField(getKey(), "lastGetStaff", this.lastGetStaff);
    }

}